package www.qiyue.ct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
public class CT_test {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("ct")
    public String test(){
        String sql="SELECT * from fee_test WHERE start_date <=mzk_data and end_date>=mzj_data";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : list) {
            String fl = map.get("fl").toString();
            Double ajust_fee = Double.parseDouble(map.get("ajust_fee")+"");
            String code = map.get("code") + "";
            String feeplan="SELECT * from reis_fee_plan WHERE `code` ='"+code+"'";
            List<Map<String, Object>> feeplanList = jdbcTemplate.queryForList(feeplan);
            if (feeplanList!=null&&feeplanList.size()==1){
                Map<String, Object> objectMap = feeplanList.get(0);
                final long id = Long.parseLong(objectMap.get("id") + "");
                Double ajustfee = Double.parseDouble(objectMap.get("ajust_fee")+"");
                Double fee = Double.parseDouble(objectMap.get("fee")+"");
                Double contract_fee = Double.parseDouble(objectMap.get("contract_fee")+"");
                if (fl.equals("未开票，未收款")){
                    String update="UPDATE reis_fee_plan set fee="+(ajust_fee+ajustfee)+",remark='符合疫情期间租金减免相关政策，进行租金减免',ajust_fee="+fee+ajust_fee+" WHERE id="+id+"";
                    jdbcTemplate.update(update);
                }else if(fl.equals("已开票，未收款")){
                    int i =1;
                    StringBuffer buffer = new StringBuffer();
                    buffer.append("INSERT INTO reis_fee_plan (");
                    objectMap.remove("id");
                    Set<String> keySet = objectMap.keySet();

                    for (String s : keySet) {
                        if (i==keySet.size()){
                            buffer.append(s+") VALUES (");
                        }else {
                            buffer.append(" "+s+",");
                        }
                    }
                    for (String s : keySet) {
                        if (i==keySet.size()){
                            buffer.append(objectMap.get(s)+")");
                        }else {
                            buffer.append(" "+objectMap.get(s)+",");
                        }
                    }
                    System.out.println(buffer.toString());
                    jdbcTemplate.update(buffer.toString());
                }
            }else {
                System.out.println(code+"有问题");
            }

        }

        return null;
    }
}
